package com.bwie.sixth.house.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.sixth.house.config.PageUtil;
import com.bwie.sixth.house.vo.OldEsVo;
import com.bwie.sixth.house.vo.RentEsQuaryVo;
import com.bwie.sixth.house.vo.RentEsVo;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.bwie.sixth.house.dao.RentHouseDao;
import com.bwie.sixth.house.entity.RentHouseEntity;
import com.bwie.sixth.house.service.RentHouseService;


@Service("rentHouseService")
public class RentHouseServiceImpl extends ServiceImpl<RentHouseDao, RentHouseEntity> implements RentHouseService {


    @Autowired
    private RestHighLevelClient client;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RentHouseEntity> page = this.page(
                new Query<RentHouseEntity>().getPage(params),
                new QueryWrapper<RentHouseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtil rentEsList(RentEsQuaryVo rentEsQuaryVo, int size, int page) throws IOException {
        SearchRequest searchRequest = new SearchRequest("rent");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (rentEsQuaryVo!=null){
            if (rentEsQuaryVo.getMapId()!=null&&rentEsQuaryVo.getMapId()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("mapId",rentEsQuaryVo.getMapId()));
            }
            if (rentEsQuaryVo.getRentMode()!=null&&rentEsQuaryVo.getRentMode()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentMode",rentEsQuaryVo.getRentMode()));
            }
            if (rentEsQuaryVo.getRentRoom()!=null&&rentEsQuaryVo.getRentRoom()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentRoom",rentEsQuaryVo.getRentRoom()));
            }
            if (rentEsQuaryVo.getRentOrient()!=null&&rentEsQuaryVo.getRentOrient()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentOrient",rentEsQuaryVo.getRentOrient()));
            }
            if (rentEsQuaryVo.getRentTerm()!=null&&rentEsQuaryVo.getRentTerm()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentTerm",rentEsQuaryVo.getRentTerm()));
            }
            if (rentEsQuaryVo.getRentFloor()!=null&&rentEsQuaryVo.getRentFloor()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentFloor",rentEsQuaryVo.getRentFloor()));
            }
            if (rentEsQuaryVo.getRentFeat()!=null&&rentEsQuaryVo.getRentFeat()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("rentFeat",rentEsQuaryVo.getRentFeat()));
            }
            if (rentEsQuaryVo.getRentSmallPrice()!=null&&rentEsQuaryVo.getRentBigPrice()!=null&&rentEsQuaryVo.getRentSmallPrice()!=0&&rentEsQuaryVo.getRentBigPrice()!=0){
                boolQueryBuilder.filter(QueryBuilders.rangeQuery("rentPrice")
                        .from(rentEsQuaryVo.getRentSmallPrice()).to(rentEsQuaryVo.getRentBigPrice()));
            }
            searchSourceBuilder.query(boolQueryBuilder);
        }
        else{
            searchSourceBuilder.query(boolQueryBuilder);
        }
        searchSourceBuilder.size(size);
        searchSourceBuilder.from((page-1)*size);

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
        long numHits = totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        SearchHit[] searchHits = hits.getHits();

        ArrayList<RentEsVo> listVos = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            // do something with the SearchHit
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            RentEsVo rentEsVo = JSON.parseObject(sourceAsString, new TypeReference<RentEsVo>() {
            });
            rentEsVo.setRentId(Integer.valueOf(id));
            listVos.add(rentEsVo);

        }
        return new PageUtil<RentEsVo>(page,numHits,listVos,size);
    }

}